// 个人中心页面交互脚本

// 等待DOM加载完成
 document.addEventListener('DOMContentLoaded', function() {
    // 角色切换功能
    const studentRoleBtn = document.getElementById('studentRoleBtn');
    const organizerRoleBtn = document.getElementById('organizerRoleBtn');
    const studentMenu = document.getElementById('studentMenu');
    const organizerMenu = document.getElementById('organizerMenu');
    const organizerContent = document.getElementById('organizer-content');
    
    // 学生角色切换
    studentRoleBtn.addEventListener('click', function() {
        // 更新按钮状态
        studentRoleBtn.classList.add('bg-primary', 'text-white');
        studentRoleBtn.classList.remove('bg-gray-100', 'text-gray-600');
        organizerRoleBtn.classList.add('bg-gray-100', 'text-gray-600');
        organizerRoleBtn.classList.remove('bg-primary', 'text-white');
        
        // 显示学生菜单和内容
        studentMenu.classList.remove('hidden');
        organizerMenu.classList.add('hidden');
        organizerContent.classList.add('hidden');
        
        // 滚动到顶部
        window.scrollTo({ top: 0, behavior: 'smooth' });
        
        // 模拟切换效果
        simulateLoading();
    });
    
    // 主办方角色切换
    organizerRoleBtn.addEventListener('click', function() {
        // 更新按钮状态
        organizerRoleBtn.classList.add('bg-primary', 'text-white');
        organizerRoleBtn.classList.remove('bg-gray-100', 'text-gray-600');
        studentRoleBtn.classList.add('bg-gray-100', 'text-gray-600');
        studentRoleBtn.classList.remove('bg-primary', 'text-white');
        
        // 显示主办方菜单和内容
        organizerMenu.classList.remove('hidden');
        studentMenu.classList.add('hidden');
        organizerContent.classList.remove('hidden');
        
        // 滚动到顶部
        window.scrollTo({ top: 0, behavior: 'smooth' });
        
        // 模拟切换效果
        simulateLoading();
    });
    
    // 侧边栏导航链接激活状态
    const sidebarLinks = document.querySelectorAll('.sidebar-link');
    sidebarLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            // 阻止默认跳转
            e.preventDefault();
            
            // 移除所有链接的激活状态
            sidebarLinks.forEach(item => {
                item.classList.remove('active');
            });
            
            // 添加当前链接的激活状态
            this.classList.add('active');
            
            // 滚动到对应的内容区域
            const targetId = this.getAttribute('href');
            const targetElement = document.querySelector(targetId);
            if (targetElement) {
                targetElement.scrollIntoView({ behavior: 'smooth' });
            }
        });
    });
    
    // 学生信息表单提交处理
    const studentForm = document.getElementById('student-info-form');
    if (studentForm) {
        studentForm.addEventListener('submit', function(e) {
            e.preventDefault();
            showToast('个人信息保存成功！');
        });
    }
    
    // 主办方信息表单提交处理
    const organizerForm = document.getElementById('organizer-info-form');
    if (organizerForm) {
        organizerForm.addEventListener('submit', function(e) {
            e.preventDefault();
            showToast('组织信息保存成功！');
        });
    }
    
    // 标签页切换
    const tabBtns = document.querySelectorAll('.tab-btn');
    tabBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            // 找到同组的标签按钮
            const parent = this.parentElement;
            const siblings = parent.querySelectorAll('.tab-btn');
            
            // 移除同组所有按钮的激活状态
            siblings.forEach(item => {
                item.classList.remove('active', 'bg-primary/10', 'text-primary');
                item.classList.add('hover:bg-gray-100');
            });
            
            // 添加当前按钮的激活状态
            this.classList.add('active', 'bg-primary/10', 'text-primary');
            this.classList.remove('hover:bg-gray-100');
        });
    });
    
    // 收藏按钮切换
    const favoriteBtns = document.querySelectorAll('.fa-star');
    favoriteBtns.forEach(btn => {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            
            // 切换收藏状态
            if (this.classList.contains('text-warning')) {
                this.classList.remove('text-warning');
                showToast('已取消收藏');
            } else {
                this.classList.add('text-warning');
                showToast('收藏成功');
            }
        });
    });
    
    // 全部标为已读
    const markAllReadBtns = document.querySelectorAll('button:text-primary');
    markAllReadBtns.forEach(btn => {
        if (btn.textContent.trim() === '全部标为已读') {
            btn.addEventListener('click', function() {
                const unreadNotifications = document.querySelectorAll('.bg-primary/5');
                unreadNotifications.forEach(notification => {
                    notification.classList.remove('bg-primary/5', 'border-primary/20');
                    notification.classList.add('border-gray-100');
                });
                showToast('已全部标为已读');
            });
        }
    });
    
    // 取消报名按钮
    const cancelRegBtns = document.querySelectorAll('.btn-danger');
    cancelRegBtns.forEach(btn => {
        if (btn.textContent.trim() === '取消报名') {
            btn.addEventListener('click', function() {
                if (confirm('确定要取消报名吗？')) {
                    const activityItem = this.closest('.border-gray-100');
                    if (activityItem) {
                        activityItem.style.opacity = '0.5';
                        showToast('报名已取消');
                        
                        // 3秒后移除元素
                        setTimeout(() => {
                            activityItem.remove();
                        }, 3000);
                    }
                }
            });
        }
    });
    
    // 关闭报名按钮
    cancelRegBtns.forEach(btn => {
        if (btn.textContent.trim() === '关闭报名') {
            btn.addEventListener('click', function() {
                if (confirm('确定要关闭活动报名吗？关闭后将不再接受新的报名。')) {
                    const badge = this.closest('.border-gray-100').querySelector('.badge');
                    if (badge) {
                        badge.textContent = '已关闭';
                        badge.classList.remove('badge-primary');
                        badge.classList.add('badge-danger');
                    }
                    showToast('已关闭报名');
                }
            });
        }
    });
    
    // 模拟加载效果
    function simulateLoading() {
        const contentArea = document.querySelector('.flex-1');
        const originalContent = contentArea.innerHTML;
        
        // 显示加载状态
        contentArea.innerHTML = `
            <div class="flex justify-center items-center py-20">
                <div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary"></div>
            </div>
        `;
        
        // 1秒后恢复内容
        setTimeout(() => {
            contentArea.innerHTML = originalContent;
        }, 500);
    }
    
    // 显示提示信息
    function showToast(message) {
        // 检查是否已存在toast
        let toast = document.querySelector('.toast-message');
        if (toast) {
            document.body.removeChild(toast);
        }
        
        // 创建新的toast
        toast = document.createElement('div');
        toast.className = 'toast-message fixed top-4 left-1/2 transform -translate-x-1/2 bg-dark text-white px-6 py-3 rounded-lg shadow-lg z-50 transition-all duration-300';
        toast.textContent = message;
        document.body.appendChild(toast);
        
        // 显示toast
        setTimeout(() => {
            toast.classList.add('opacity-100');
            toast.classList.remove('opacity-0');
        }, 10);
        
        // 3秒后隐藏toast
        setTimeout(() => {
            toast.classList.add('opacity-0');
            toast.classList.remove('opacity-100');
            setTimeout(() => {
                if (document.body.contains(toast)) {
                    document.body.removeChild(toast);
                }
            }, 300);
        }, 3000);
    }
    
    // 头像上传预览
    const avatarInputs = document.querySelectorAll('input[type="file"]');
    avatarInputs.forEach(input => {
        input.addEventListener('change', function(e) {
            if (e.target.files && e.target.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const imgElement = input.closest('.relative').querySelector('img');
                    if (imgElement) {
                        imgElement.src = e.target.result;
                        showToast('头像上传成功');
                    }
                };
                reader.readAsDataURL(e.target.files[0]);
            }
        });
    });
    
    // 初始化页面滚动位置
    window.scrollTo(0, 0);
    
    // 添加页面载入动画
    document.body.classList.add('loaded');
});

// 通知详情点击处理
function viewNotificationDetail(notificationId) {
    alert('查看通知详情: ' + notificationId);
    // 这里可以实现跳转到通知详情页面的逻辑
}

// 活动详情点击处理
function viewActivityDetail(activityId) {
    window.location.href = 'activity-detail.html?id=' + activityId;
}

// 编辑活动处理
function editActivity(activityId) {
    window.location.href = 'publish-activity.html?id=' + activityId + '&edit=true';
}

// 分享活动处理
function shareActivity(activityId) {
    alert('分享活动: ' + activityId);
    // 这里可以实现分享功能的逻辑
}